<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="主键ID" prop="id">
                    <el-input v-model="formData.id" placeholder="请输入主键ID" />
                </el-form-item>
                <el-form-item label="用户名" prop="usersName">
                    <el-input v-model="formData.usersName" placeholder="请输入用户名" />
                </el-form-item>
                <el-form-item label="联系人" prop="contacts">
                    <el-input v-model="formData.contacts" placeholder="请输入联系人" />
                </el-form-item>
                <el-form-item label="手机号" prop="phone">
                    <el-input v-model="formData.phone" placeholder="请输入手机号" />
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="formData.email" placeholder="请输入邮箱" />
                </el-form-item>
                <el-form-item label="用户类型编号" prop="userTypeId">
                    <el-input v-model="formData.userTypeId" placeholder="请输入用户类型编号" />
                </el-form-item>
                <el-form-item label="用户类型" prop="userType">
                    <el-select
                        class="flex-1"
                        v-model="formData.userType"
                        placeholder="请选择用户类型"
                    >
                        <el-option label="请选择字典生成" value="" />
                    </el-select>
                </el-form-item>
                <el-form-item label="创建时间" prop="createsTime">
                    <el-input v-model="formData.createsTime" placeholder="请输入创建时间" />
                </el-form-item>
                <el-form-item label="修改人编号" prop="updateUserId">
                    <el-input v-model="formData.updateUserId" placeholder="请输入修改人编号" />
                </el-form-item>
                <el-form-item label="修改人名称" prop="updateUserName">
                    <el-input v-model="formData.updateUserName" placeholder="请输入修改人名称" />
                </el-form-item>
                <el-form-item label="操作人ID" prop="userId">
                    <el-input v-model="formData.userId" placeholder="请输入操作人ID" />
                </el-form-item>
                <el-form-item label="操作人名称" prop="userName">
                    <el-input v-model="formData.userName" placeholder="请输入操作人名称" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { platformuserEdit, platformuserAdd, platformuserDetail } from '@/api/platformuser'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑平台用户' : '新增平台用户'
})

const formData = reactive({
    id: '',
    usersName: '',
    contacts: '',
    phone: '',
    email: '',
    userTypeId: '',
    userType: '',
    createsTime: '',
    updateUserId: '',
    updateUserName: '',
    userId: '',
    userName: ''
})

const formRules = {}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await platformuserEdit(data) : await platformuserAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await platformuserDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
